<template>
	<view class="selectaddress">
		<view class="topbar"></view>
		<view class="top">
			<uni-icons class="back" type="back" size="30" @click="back()"></uni-icons>
			<view>选择收货地址</view>
		</view>
		<view class="toptop"></view>
		<view class="location">
			<uni-icons type="location" size="30"></uni-icons>
			<view>{{province}}{{city}}</view>
			<uni-icons type="right" size="30"></uni-icons>
			<view @click="getlo()">重新定位</view>
		</view>
		<view class="list">
			<view class="form"  :class="[lid == index ?'active':'']" @click="borderColor(index),tosettle()" v-for="(item,index) in locationlist" :key="index">
				<view>
					<text>{{item.name}}</text>
					<text class="phone">{{item.phone}}</text>
				</view>
				<view>收货地址：{{item.address+item.houseNum}}</view>
			</view>
		</view>
		<view class="bottom"></view>
		
		<view class="add">
			<button @click="toadd()">
				<image src="https://cdn3.axureshop.com/demo/2116044/images/%E7%BB%93%E7%AE%97/u2490.png" mode=""></image>
				添加地址
			</button>
		</view>
	</view>
</template>

<script>
	import http from '@/utils/http.js' 
	export default {
		data() {
			return {
				state:'',
				userid:'1',
				locationlist:[],
				lid:'0',
				selectlid:'',
				latitude:'',
				longitude:'',
				position:[],
				province:'',
				city:''
			}
		},
		methods: {
			back(){
				// let pages = getCurrentPages();
				// console.log(pages);
				// let backPages = pages.length - 1;
				// uni.navigateBack({
				// 	delta:backPages,
				uni.navigateTo({
					url:'/pages/settle/settle'
				})
				// })
			},
			borderColor(index){
				console.log(index)
				this.lid = index;
				this.selectlid = this.locationlist[index].id;
				uni.setStorage({
					key:'selectlid',
					data:{selectlid:this.selectlid}
				})
			},
			toadd(){
				uni.navigateTo({
					url:"/pages/addaddress/addaddress"
				})
			},
			tosettle(){
				uni.navigateTo({
					url:"/pages/settle/settle"
				})
			},
			getlo(){
				uni.getLocation({
					type: 'gcj02',
					geocode:true,
					success: res=> {
						// console.log(res);
						console.log('当前位置的经度：' + res.longitude);
						console.log('当前位置的纬度：' + res.latitude);
						this.latitude=res.latitude;
						this.longitude=res.longitude;
					}
				});
			}
		},
		onLoad(){
			// uni.request({
			// 	url:"https://api.jisuapi.com/geoconvert/coord2addr",
			// 	data:{
			// 		lat:this.latitude,
			// 		lng:this.longitude,
			// 		type:"baidu",
			// 		appkey:"1b72844d47922af1"
			// 	},
			// 	success:function(res){
			// 		console.log(res);
			// 		// this.location = res.data.result;
			// 		// console.log(this.location);
			// 	}
			// })
		},
		onShow(){
			http({
				url:'/sellocation',
				method:'post',
				data:{
					userid:this.userid,
				},
				success: (res) => {
					console.log(res);
					this.locationlist = res.data;
					console.log(this.locationlist);
				}
			});
		},
		watch:{
			latitude:function(){
				var that = this;
				uni.request({
					url:"https://api.jisuapi.com/geoconvert/coord2addr",
					data:{
						lat:this.latitude,
						lng:this.longitude,
						type:"baidu",
						appkey:"1b72844d47922af1"
					},
					success:function(res){
						console.log(res);
						that.position = res.data.result;
						that.province = res.data.result.province;
						that.city = res.data.result.city;
						// console.log(this.location);
					}
				})
			}
		}
	}
</script>

<style>
page{
	background-color: #F2F2F2;
}
.topbar{
	width: 100%;
	background-color: #ffffff;
}
.toptop{
	height: 120rpx;
}
.top{
	padding-top: 60rpx;
	padding-bottom: 20rpx;
	display: flex;
	justify-content: center;
	font-size: 30rpx;
	background-color: #ffffff;
	position: fixed;
	width: 100%;
}
.top>.back{
	position: absolute;
	left: 0;
	zoom: 0.8;
}
/* .selectaddress{
	width: 100%;
	background-color: #F2F2F2;
} */
.location{
	display: flex;
	padding-top: 20rpx;
	padding-bottom: 20rpx;
	font-size: 30rpx;
	background-color: #ffffff;
	position: fixed;
	width: 100%;
}
.location>uni-icons:nth-child(1){
	zoom: 0.65;
}
.location>uni-icons:nth-child(3){
	zoom: 0.5;
	margin-top: 15rpx;
}
.location>view:nth-child(4){
	position: absolute;
	right: 25rpx;
	color: #FA746B;
}
.list{
	margin-top: 100rpx;
}
.list>.form{
	margin-top: 35rpx;
	margin-left: 30rpx;
	border-radius: 15rpx;
	width: 690rpx;
	height: 160rpx;
	background-color: #ffffff;
}
.list .form.active {
	border: #FA746B 1rpx solid;
}
.list .form view{
	padding-left: 30rpx;
}
.list .form view .phone{
	padding-top: 8rpx;
	position: absolute;
	left: 150rpx;
}
.list .form view:nth-child(1){
	padding-top: 25rpx;
}
.list .form view:nth-child(2){
	padding-top: 25rpx;
	font-size: 25rpx;
	color: #999999;
}
.list>.form view text{
	font-size: 26rpx;
	font-weight: bold;
}
.list>.form view text:nth-child(2){
	padding-left: 30rpx;
}
.bottom{
	height: 130rpx;
}
.add{
	width: 100%;
	height: 130rpx;
	background-color: #ffffff;
	position: fixed;
	bottom: 0;
}
.add button{
	width: 650rpx;
	background-color: #ffffff;
	color: #FA746B;
	font-size: 30rpx;
	margin-top: 20rpx;
	border: 1rpx solid #FA746B;
	border-radius: 50rpx;
}
.add button image{
	width: 25rpx;
	height: 25rpx;
	margin-right: 10rpx;
}
</style>
